﻿using System;
using System.Data;
using System.Data.SqlClient;
using prjKQXS_DLL;

namespace prjKQXS_DLL.Ve_so
{
    public class DMKETQUA_DAL
    {
        public int InsertDMKQ(DMKETQUA dmve)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            conn.Open();
            SqlCommand sqlCommand1 = new SqlCommand("Delete from Ketqua Where Ngaymothuong = @Ngaymothuong", conn);
            sqlCommand1.Parameters.AddWithValue("@Ngaymothuong", dmve.Ngaymothuong);
            sqlCommand1.ExecuteNonQuery();
            SqlCommand sqlCommand = new SqlCommand("insert into [Ketqua](Ngaymothuong,Kyhieu,Giave,gdacbiet,gnhat,gnhi1,gnhi2,gba1,gba2,gba3,gba4,gba5,gba6,gtu1,gtu2,gtu3,gtu4,gnam1,gnam2,gnam3,gnam4,gnam5,gnam6,gsau1,gsau2,gsau3,gbay1,gbay2,gbay3,gbay4) values(@Ngaymothuong,@Kyhieu,@Giave,@gdacbiet,@gnhat,@gnhi1,@gnhi2,@gba1,@gba2,@gba3,@gba4,@gba5,@gba6,@gtu1,@gtu2,@gtu3,@gtu4,@gnam1,@gnam2,@gnam3,@gnam4,@gnam5,@gnam6,@gsau1,@gsau2,@gsau3,@gbay1,@gbay2,@gbay3,@gbay4)", conn);
            try
            {
                sqlCommand.Parameters.AddWithValue("@Giave", dmve.Giave);
                sqlCommand.Parameters.AddWithValue("@Kyhieu", dmve.Kyhieu);
                sqlCommand.Parameters.AddWithValue("@Ngayquayso", dmve.Ngaymothuong);
                sqlCommand.Parameters.AddWithValue("@gdacbiet", dmve.gdacbiet);
                sqlCommand.Parameters.AddWithValue("@gnhat", dmve.gnhat);
                sqlCommand.Parameters.AddWithValue("@gnhi1", dmve.gnhi1);
                sqlCommand.Parameters.AddWithValue("@gnhi2", dmve.gnhi2);
                sqlCommand.Parameters.AddWithValue("@gba1", dmve.gba1);
                sqlCommand.Parameters.AddWithValue("@gba2", dmve.gba2);
                sqlCommand.Parameters.AddWithValue("@gba3", dmve.gba3);
                sqlCommand.Parameters.AddWithValue("@gba4", dmve.gba4);
                sqlCommand.Parameters.AddWithValue("@gba5", dmve.gba5);
                sqlCommand.Parameters.AddWithValue("@gba6", dmve.gba6);
                sqlCommand.Parameters.AddWithValue("@gtu1", dmve.gtu1);
                sqlCommand.Parameters.AddWithValue("@gtu2", dmve.gtu2);
                sqlCommand.Parameters.AddWithValue("@gtu3", dmve.gtu3);
                sqlCommand.Parameters.AddWithValue("@gtu4", dmve.gtu4);
                sqlCommand.Parameters.AddWithValue("@gnam1", dmve.gnam1);
                sqlCommand.Parameters.AddWithValue("@gnam2", dmve.gnam2);
                sqlCommand.Parameters.AddWithValue("@gnam3", dmve.gnam3);
                sqlCommand.Parameters.AddWithValue("@gnam4", dmve.gnam4);
                sqlCommand.Parameters.AddWithValue("@gnam5", dmve.gnam5);
                sqlCommand.Parameters.AddWithValue("@gnam6", dmve.gnam6);
                sqlCommand.Parameters.AddWithValue("@gsau1", dmve.gsau1);
                sqlCommand.Parameters.AddWithValue("@gsau2", dmve.gsau2);
                sqlCommand.Parameters.AddWithValue("@gsau3", dmve.gsau3);
                sqlCommand.Parameters.AddWithValue("@gbay1", dmve.gbay1);
                sqlCommand.Parameters.AddWithValue("@gbay2", dmve.gbay2);
                sqlCommand.Parameters.AddWithValue("@gbay3", dmve.gbay3);
                sqlCommand.Parameters.AddWithValue("@gbay4", dmve.gbay4);
                return sqlCommand.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                sqlCommand.Dispose();
                sqlCommand1.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
        public string SelectDMKQ(string tenCot, DateTime Ngaymothuong)
        {
            string kq = "";
            SqlConnection conn = new SqlConnection(BienTc.connString);
            conn.Open();
            SqlCommand sqlCommand1 = new SqlCommand("select " + tenCot.Trim() + " from Ketqua Where Ngaymothuong = @Ngaymothuong", conn);
            sqlCommand1.Parameters.AddWithValue("@Ngaymothuong", Ngaymothuong);
            SqlDataReader dr = sqlCommand1.ExecuteReader();
            try
            {
                if (dr.Read())
                {
                    kq = (string)dr[tenCot];
                }
                return kq;
            }
            catch
            {
                throw;
            }
            finally
            {

                sqlCommand1.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
    }
}
